<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Music
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: AdminLevelController.php 7940 2010-12-05 20:10:31Z char $
 * @author     Steve
 */

/**
 * @category   Application_Extensions
 * @package    Music
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class Music_AdminLevelController extends Core_Controller_Action_Admin {
	public function init() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'music_admin_main', array (), 'music_admin_main_level' );
	}
	
	public function indexAction() {
		// Get level id
		if (null !== ($id = $this->_getParam ( 'level_id', $this->_getParam ( 'id' ) ))) {
			$level = Engine_Api::_ ()->getItem ( 'authorization_level', $id );
		} else {
			$level = Engine_Api::_ ()->getItemTable ( 'authorization_level' )->getDefaultLevel ();
		}
		
		if (! $level instanceof Authorization_Model_Level) {
			throw new Engine_Exception ( 'missing level' );
		}
		
		$id = $level->level_id;
		
		// Make form
		$this->view->form = $form = new Music_Form_Admin_Settings_Level ( array ('public' => (in_array ( $level->type, array ('public' ) )), 'moderator' => (in_array ( $level->type, array ('admin', 'moderator' ) )) ) );
		$form->level_id->setValue ( $id );
		
		$permissionsTable = Engine_Api::_ ()->getDbtable ( 'permissions', 'authorization' );
		$form->populate ( $permissionsTable->getAllowed ( 'music_playlist', $id, array_keys ( $form->getValues () ) ) );
		
		// Check post
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		// Check validitiy
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		// Process
		

		$values = $form->getValues ();
		
		$db = $permissionsTable->getAdapter ();
		$db->beginTransaction ();
		
		try {
			// Set permissions
			$permissionsTable->setAllowed ( 'music_playlist', $id, $values );
			
			// Commit
			$db->commit ();
		} 

		catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		$form->addNotice ( 'Your changes have been saved.' );
	}
}